/*
 *  $Id: Legion.cs 1051 2010-05-18 22:10:41Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

namespace Ragima.Cards001.Water.Spells {
    public class Legion: SpellCard<Affect> {
        public Legion(): base("001_w_s_legion") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Water;
            Value = 100;
            Target = new EnchantmentTarget(GetTargetPhrase(), true, OnlyOwnCreature);
            AddManaCost(ElementType.Water, 3);
        }

        private static bool OnlyOwnCreature(TargetRequest request, Affect affect) {
            return affect.Owner is Creature && affect.Owner.Owner == request.Player;
        }

        private static readonly Languages.CardPhrase<Legion> NoTargets = new Languages.CardPhrase<Legion>("no_targets");

        protected override bool IsPossibleAction(TargetArgs<Affect> args) {
            List<Creature> targets = GetTargets(args.Player, args.Target.Owner);
            if (targets.Count == 0) {
                args.Player.Game.ErrorNotice(NoTargets.Phrase.GetText());
                return false;
            }
            int mana = 3 + targets.Count;
            if (args.Player.GetMana(ElementType.Water) < mana) {
                args.Player.Game.ErrorNotice(ManaCondition.NotEnough.GetText(ElementType.Water));
                return false;
            }
            return true;
        }

        protected override void DoAction(TargetArgs<Affect> args) {
            List<Creature> targets = GetTargets(args.Player, args.Target.Owner);
            args.Player.DecrementMana(ElementType.Water, targets.Count);
            foreach (Creature cr in targets)
                cr.AddAffect(args.Target.Clone());
        }

        private static List<Creature> GetTargets(Player owner, AbstractObject source) {
            List<Creature> list = new List<Creature>();
            foreach (Creature cr in owner.GetCreatures())
                if (cr != source && !cr.HasFlag<Affects.Antimagic>())
                    list.Add(cr);
            return list;
        }
    }
}
